package miblab.school;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.Date;

import android.util.Log;

public class SystemFunctions {


	/************************Private Variables**************/

	private static final String TAG = null;

	/************************Public functions**************/

	//-------------GET FILENAME------------------------------//
	
	/**************DATE TIME************************-*/
	
	public final static String Getfilename()
	{		String filename = null;
	//To Write to an SD
	try
	{
		String date = DateFormat.getDateInstance().format(new Date());
		filename=date+".txt";

	}
	catch(Exception e)
	{
		Log.i(TAG, "No Date"); 
	}

	return filename;
	}

	//--------------PARSE STRING-------------------------------

	public  final static double[] ParseString(String filename)
	{

		String aDataRow = "";
		String strlat;
		String strlon;
		String strtim;
		String strloc = null,strprev = null;
		int i=0;
		double[] timespent= new double[4];
		double[]temp= new double[4];
		double[] loc= new double[3];
		double[] locprev= new double[3];

		for(int j=0;j<4;j++)
			timespent[j]=0;

		try
		{
			{	 
				File gpxfile = new File(filename);
				FileInputStream fIn = new FileInputStream(gpxfile);
				if(!gpxfile.exists())
				{
					Log.i(TAG, "File Error");
				}
				BufferedReader myReader = new BufferedReader(
						new InputStreamReader(fIn));

				while ((aDataRow = myReader.readLine()) != null) 
				{

					for(int j=0;j<3;j++)
						locprev[j]=loc[j];
					String[] tokens = aDataRow.split(":");
					if(strloc!=null)
					{
						strprev=strloc;

					}
					for(String t : tokens)
					{	

						i++;
						if(i==2)
						{	
							strlat=t;
							try {
								loc[0]=Double.parseDouble(strlat);
							} catch (NumberFormatException e) {
								Log.i(TAG, "Wrong Number 1");
								e.printStackTrace();
							}
						}

						if(i==4)
						{
							strlon=t;
							try {
								loc[1]=Double.parseDouble(strlon);
							} catch (NumberFormatException e) {
								Log.i(TAG, "Wrong Number 2");
								e.printStackTrace();
							}
						}

						if(i==6)
						{
							strtim=t;
							try {
								loc[2]=Double.parseDouble(strtim);

							} catch (NumberFormatException e) {
								Log.i(TAG, "Wrong Number 3");
								e.printStackTrace();
							}	        

						}	
						if(i==8)
						{
							strloc=t;
						}	
					}	
					if((strloc!=null)&&(strprev!=null))
					{
						temp=CheckTimes(locprev,loc,strloc,strprev);
						i=0;	
						for(int j=0;j<4;j++)
							timespent[j]=timespent[j]+temp[j];

					}

				}
				myReader.close();
			}

		} 
		catch(Exception e)
		{
			e.printStackTrace();
		} 
		return timespent;

	}

	//------------------------CHECK TIMES--------------------------------------------
	public final static double[] CheckTimes(double[] lp,double[] l,String p,String pp)
	{

		double[] times=new double[4];
		if(p.equals(pp))
		{	

			if(p.equals("Home"))
			{
				times[0]=(l[2]-lp[2]);
			}
			else if(p.equals("School"))
			{
				times[1]=(l[2]-lp[2]);
			}
			else if(p.equals("Hospital"))
			{
				times[2]=(l[2]-lp[2]);
			}
			else
			{
				times[3]=(l[2]-lp[2]);
			}

		} 
		else
		{
			if(p.equals("Home"))
			{

				if(pp.equals("School"))
				{
					times[0]=((l[2]-lp[2])/2);
					times[1]=((l[2]-lp[2])/2);
				}
				else if(pp.equals("Hospital"))
				{
					times[0]=((l[2]-lp[2])/2);
					times[2]=((l[2]-lp[2])/2);
				}
				else 
				{
					times[0]=((l[2]-lp[2])/2);
					times[3]=((l[2]-lp[2])/2);
				}

			}
			if(p.equals("School"))
			{	if(pp.equals("Home"))
			{
				times[1]=((l[2]-lp[2])/2);
				times[0]=((l[2]-lp[2])/2);
			}
			else if(pp.equals("Hospital"))
			{
				times[1]=((l[2]-lp[2])/2);
				times[2]=((l[2]-lp[2])/2);
			}
			else
			{
				times[1]=((l[2]-lp[2])/2);
				times[3]=((l[2]-lp[2])/2);
			}

			}
			if(p.equals("Hospital"))
			{

				if(pp.equals("School"))
				{
					times[2]=((l[2]-lp[2])/2);
					times[1]=((l[2]-lp[2])/2);
				}
				else if(pp.equals("Home"))
				{
					times[2]=((l[2]-lp[2])/2);
					times[0]=((l[2]-lp[2])/2);
				}
				else
				{
					times[2]=((l[2]-lp[2])/2);
					times[3]=((l[2]-lp[2])/2);
				}

			}
			if(p.equals("Others"))
			{

				if(pp.equals("Home"))
				{
					times[3]=((l[2]-lp[2])/2);
					times[0]=((l[2]-lp[2])/2);
				}
				else if(pp.equals("Hospital"))
				{
					times[3]=((l[2]-lp[2])/2);
					times[2]=((l[2]-lp[2])/2);
				}
				else
				{
					times[3]=((l[2]-lp[2])/2);
					times[1]=((l[2]-lp[2])/2);
				}

			}
		}
		return times;  

	}
	
	//**********************************GET LAT N LON***************************************************************
	public final static double[] LatLon(String filename)
	{

		String aDataRow = "";
		String strlat;
		String strlon;
		int i=0;
		double[] loc= new double[2];
		try
		{
			File root = new File(SystemParameters.path, "School");
			if (!root.exists())
			{
				root.mkdirs();
			}

			if(root.canWrite())
			{	 
				File gpxfile = new File(root,filename);
				FileInputStream fIn = new FileInputStream(gpxfile);
				BufferedReader myReader = new BufferedReader(
						new InputStreamReader(fIn));

				while ((aDataRow = myReader.readLine()) != null) 
				{
					String[] tokens = aDataRow.split(":");
					for(String t : tokens)
					{
						i++;
						if(i==2)
						{	
							strlat=t;

							try {
								loc[0]=Double.parseDouble(strlat);
							} catch (NumberFormatException e) {
								Log.i(TAG, "Wrong Number 1");
								
								e.printStackTrace();
							}
						}
						if(i==4)
						{
							strlon=t;
							try {
								loc[1]=Double.parseDouble(strlon);
							} catch (NumberFormatException e) {
								Log.i(TAG, "Wrong Number 2");
								
								e.printStackTrace();
							}
						}	

					}			
				}
				myReader.close();

			}

		}
		catch(IOException e)
		{
			e.printStackTrace();
		} 
		return loc;

	}
}
